package com.hucii.app.service;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningTaskInfo;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Handler;
import android.os.Message;

import com.hucii.app.R;
import com.hucii.app.entity.UpdateInfo;
import com.hucii.app.json.handler.UpdateHandler;
import com.hucii.app.net.HttpTools;

public class UpdateCheckService implements Runnable {

	UpdateAPKService updateAPK;
	public static Context context;
	String clientname = null;
	String currentversion = null;
	String url = "http://zhanqi.net/fang/update";

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {

			UpdateInfo info = (UpdateInfo) msg.obj;
			if (info.updateflag == 1 && getTopActivity()) {// 手动升级
				// 手动刷新
				showMyDialog(info);
			} else if (info.updateflag == 2) {// 强制升级
				update(info.updateurl);
			}
			super.handleMessage(msg);
		}
	};

	public UpdateCheckService(Context c) {

		context = c;
		clientname = context.getString(R.string.app_name_code);
		try {
			currentversion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		url += "?clientname=" + clientname + "&version=" + currentversion;
	}

	public void start() {
		new Thread(this).start();
	}

	public boolean getTopActivity() {
		ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
		List<RunningTaskInfo> forGroundActivity = activityManager.getRunningTasks(1);
		RunningTaskInfo currentActivity;
		currentActivity = forGroundActivity.get(0);
		String activityName = currentActivity.topActivity.getClassName();
		return activityName.contains("com.angel.scaner");

	}

	public void showMyDialog(final UpdateInfo info) {

		new AlertDialog.Builder(context).setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				//
				dialog.dismiss();
				update(info.updateurl);
			}
		}).setNegativeButton("下次", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		}).setMessage("最新版本：" + info.newversion + "\n\r" + info.descrition).setTitle("客户端更新").create().show();
	}

	public void update(String url) {

		Intent intent = new Intent();
		intent.putExtra("url", url);
		intent.setClass(context, UpdateAPKService.class);
		context.startService(intent);
	}

	@Override
	public void run() {

		UpdateInfo info = (UpdateInfo) HttpTools.getObjectWithJson(url, new UpdateHandler());
		if (info != null) {
			//
			Message msg = handler.obtainMessage();
			msg.obj = info;
			msg.sendToTarget();
		}

	}

}
